export interface LibericaJavaVersion {
  GA: boolean;
  LTS: boolean;
  featureVersion: number;
}

export async function getLibericaJavaVersions(): Promise<LibericaJavaVersion[]> {
  try {
    const resp = await native.http.getJson("https://api.bell-sw.com/v1/liberica/versions");
    if (__DEV__) {
      console.log("getLibericaJavaVersions", resp.data);
    }
    return resp.data;
  } catch (e: any) {
    console.error("getLibericaJavaVersions error", e);
    return [];
  }
}

export async function getLibericaBundleTypes(): Promise<string[]> {
  try {
    const resp = await native.http.getJson<string[]>("https://api.bell-sw.com/v1/liberica/bundle-types");
    if (__DEV__) {
      console.log("getLibericaBundleTypes", resp.data);
    }
    return resp.data;
  } catch (e: any) {
    console.error("getLibericaBundleTypes error", e);
    return [];
  }
}

// {
//   "bitness": 64,
//   "latestLTS": true,
//   "updateVersion": 1,
//   "downloadUrl": "https://github.com/bell-sw/Liberica/releases/download/21.0.1+12/bellsoft-jdk21.0.1+12-windows-aarch64-full.msi",
//   "latestInFeatureVersion": false,
//   "LTS": true,
//   "bundleType": "jdk-full",
//   "featureVersion": 21,
//   "packageType": "msi",
//   "FX": true,
//   "GA": true,
//   "architecture": "arm",
//   "latest": false,
//   "extraVersion": 0,
//   "buildVersion": 12,
//   "EOL": true,
//   "os": "windows",
//   "interimVersion": 0,
//   "version": "21.0.1+12",
//   "sha1": "46e76319568d559b1972ba7c6ccf8a763ccb73e2",
//   "filename": "bellsoft-jdk21.0.1+12-windows-aarch64-full.msi",
//   "installationType": "installer",
//   "size": 216875008,
//   "patchVersion": 0,
//   "TCK": true,
//   "updateType": "psu"
// }

export interface LibericaJdkVersionInfo {
  bitness: 64 | 32;
  latestLTS: boolean;
  updateVersion: number;
  downloadUrl: string;
  latestInFeatureVersion: boolean;
  LTS: boolean;
  bundleType: string;
  featureVersion: number;
  packageType: string;
  FX: boolean;
  GA: boolean;
  architecture: string;
  latest: boolean;
  extraVersion: number;
  buildVersion: number;
  EOL: boolean;
  os: string;
  interimVersion: number;
  version: string;
  sha1: string;
  filename: string;
  installationType: string;
  size: number;
  patchVersion: number;
  TCK: boolean;
  updateType: string;
}

export function getLibericaOsName() {
  return native.process.platform === "win32" ? "windows" : native.process.platform;
}

export function getLibericaArch() {
  switch (native.process.arch) {
    case "arm":
    case "arm64":
      return "arm";

    case "ia32":
    case "x64":
      return "x86";

    case "ppc64":
      return "ppc";

    case "riscv64":
      return "riscv";
  }

  return "";
}

const bundleTypeLabels: Dict<string> = {
  jdk: "Standard JDK",
  "jdk-crac": "CRaC JDK",
  "jdk-full": "Full JDK",
  "jdk-lite": "Lite JDK",
  "jdk-perf": "Performance JDK",
  jre: "Standard JRE",
  "jre-full": "Full JRE",
  "jre-perf": "Performance JRE",
};

export const getLibericaBundleTypeLabel = (type: string) => bundleTypeLabels[type] || type;
